﻿using System.Windows.Forms;
using SF.Code.Extension.WinAPI;

namespace SG.Ling.AutoTranslator.App
{
    partial class Program
    {
        private void OnWindowMessage(object sender, WinProcMessageEventArgs e)
        {
            if (TranslatorForm == null) return;

            var application = CommandGroup.Application.ValidateCommand(e);

            var claimed = ClaimType.Complete;
            switch (application)
            {
                case Command.Show:
                    TranslatorForm.Visible = true;

                    TranslatorForm.WindowState = FormWindowState.Normal;
                    TranslatorForm.Show();
                    TranslatorForm.Activate();
                    
                    break;

                case Command.Hide:
                    TranslatorForm.Visible = false;
                    break;

                case Command.Translate:
                    if (!string.IsNullOrWhiteSpace(e.Args))
                        TranslatorForm.TranslateAsync(e.Args);

                    break;

                case Command.Close:
                    claimed = ClaimType.NoBaseForward;
                    Close();
                    break;

                default:
                    claimed = ClaimType.None;
                    break;
            }
            e.Claimed = claimed;
        }

        private void OnLogoffMessage(object sender, WinProcMessageEventArgs e)
        {
            switch ((WindowMessage)e.Message.Msg)
            {
                case WindowMessage.WM_QUERYENDSESSION:
                case WindowMessage.WM_ENDSESSION:
                    Close();
                    break;
            }

        }
    }
}
